/*
 * benmark.c
 *
 *  Created on: Dec 19, 2010
 *      Author: cumeo89
 */

#include "benmark.h"
#include "lcstr.h"
#include <stdlib.h>
#include <stdio.h>
#include <glib.h>
#include <time.h>

//int benmark(int maxLength, int step) {
//	char *longString = malloc(maxLength+1);
//	const char *strings[] = { longString, NULL };
//	long i, j;
//	GRand *rand = g_rand_new();
//	for (i = step, j = 0; i <= maxLength; i += step) {
//		for ( ; j < i; j++) {
//			longString[j] = g_rand_int_range(rand, 'a', 'z'+1);
//		}
//		longString[j] = 0;
//
//		clock_t start = clock();
//		SuffixTree *tree = t_new(strings);
//		clock_t end = clock();
//		double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
//		t_free(tree);
//
//		printf("%ld\t%.2f\n", i, cpu_time_used);
//	}
//	g_rand_free(rand);
//	free(longString);
//	return 0;
//}

int benmark(int maxLength, int step) {
	#define MAX_K 5
	long i, j, k, count;

	char *strings[MAX_K+1];
	for (k = 0; k < MAX_K; k++) {
		strings[k] = malloc(maxLength+1);
	}
	strings[MAX_K] = NULL;

	GRand *rand = g_rand_new();
	for (i = step, j = 0, count = 1; i <= maxLength; i += step, count++) {
		for ( ; j < i; j++) {
			for (k = 0; k < MAX_K; k++) {
				strings[k][j] = g_rand_int_range(rand, 'a', 'z'+1);
			}
		}
		for (k = 0; k < MAX_K; k++) {
			strings[k][j] = 0;
		}

		printf("%ld", count);

		char *currentStrings[MAX_K+1];
		currentStrings[0] = strings[0];
		for (k = 2; k <= MAX_K; k++) {
			currentStrings[k-1] = strings[k-1];
			currentStrings[k] = NULL;

			clock_t start = clock();
			CommonString *cs = find_lcstr((const char **)currentStrings);
			clock_t end = clock();
			double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
			cs_free(cs, k);

			printf("\t%.2f", cpu_time_used);
		}

		printf("\n");
	}

	for (i = 0; i < MAX_K; i++) {
		free(strings[i]);
	}
	g_rand_free(rand);
	return 0;
}
